package com.vincent.filepicker.adapter;


import com.vincent.filepicker.ResourceTable;
import com.vincent.filepicker.ToastUtil;
import com.vincent.filepicker.Util;
import com.vincent.filepicker.ability.VideoPickAbility;
import com.vincent.filepicker.filter.entity.VideoFile;
import com.vincent.filepicker.video.VideoAbility;
import com.vincent.filepicker.video.VideoRecordAbility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;

import static com.vincent.filepicker.Constant.REQUEST_CODE_TAKE_VIDEO;

/**
 * Created by Vincent Woo
 * Date: 2016/10/21
 * Time: 14:13
 */

public class VideoPickAdapter extends BaseAdapter<VideoFile> {
    private boolean isNeedCamera;
    private int mMaxNumber;
    private int mCurrentNumber = 0;
    public String mVideoPath;
    private final int mItemMargin = 6;
    private final int mItemMarginWidth = 15;

    public VideoPickAdapter(Context ctx, boolean needCamera, int max) {
        this(ctx, new ArrayList<VideoFile>(), needCamera, max);
    }

    public VideoPickAdapter(Context ctx, ArrayList<VideoFile> list, boolean needCamera, int max) {
        super(ctx, list);
        isNeedCamera = needCamera;
        mMaxNumber = max;
    }

    @Override
    public int getCount() {
        return isNeedCamera ? mList.size() + 1 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        VideoPickViewHolder viewHolder = null;
        if (component == null) {
            viewHolder = new VideoPickViewHolder();
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_vw_layout_item_video_pick, null, false);
            viewHolder.mIvCamera = (Image) component.findComponentById(ResourceTable.Id_iv_camera);
            viewHolder.mIvThumbnail = (Image) component.findComponentById(ResourceTable.Id_iv_thumbnail);
            viewHolder.mShadow = component.findComponentById(ResourceTable.Id_shadow);
            viewHolder.mCbx = (Checkbox) component.findComponentById(ResourceTable.Id_cbx);
            viewHolder.mDuration = (Text) component.findComponentById(ResourceTable.Id_txt_duration);
            viewHolder.mDurationLayout = (DependentLayout) component.findComponentById(ResourceTable.Id_layout_duration);
            component.setTag(viewHolder);
        } else {
            viewHolder = (VideoPickViewHolder) component.getTag();
        }
        ComponentContainer.LayoutConfig params = component.getLayoutConfig();
        if (params != null) {
            int width = mContext.getResourceManager().getDeviceCapability().width
                    * mContext.getResourceManager().getDeviceCapability().screenDensity / 160;
            ComponentContainer.LayoutConfig layoutConfig = component.getLayoutConfig();
            layoutConfig.width = width / VideoPickAbility.COLUMN_NUMBER - mItemMarginWidth;
            layoutConfig.height = width / VideoPickAbility.COLUMN_NUMBER - mItemMarginWidth;
            layoutConfig.setMargins(mItemMargin, mItemMargin, mItemMargin, mItemMargin);
            component.setLayoutConfig(layoutConfig);
        }

        if (isNeedCamera && position == 0) {
            viewHolder.mIvCamera.setVisibility(Component.VISIBLE);
            viewHolder.mIvThumbnail.setVisibility(Component.INVISIBLE);
            viewHolder.mCbx.setVisibility(Component.INVISIBLE);
            viewHolder.mShadow.setVisibility(Component.INVISIBLE);
            viewHolder.mDurationLayout.setVisibility(Component.INVISIBLE);
            component.setClickedListener(component1 -> {
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(mContext.getBundleName())
                        .withAbilityName(VideoRecordAbility.class.getName())
                        .build();
                Intent intent = new Intent();
                intent.setOperation(operation);
                ((Ability) mContext).startAbilityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
            });
        } else {
            viewHolder.mIvCamera.setVisibility(Component.INVISIBLE);
            viewHolder.mIvThumbnail.setVisibility(Component.VISIBLE);
            viewHolder.mCbx.setVisibility(Component.VISIBLE);
            viewHolder.mDurationLayout.setVisibility(Component.VISIBLE);

            final VideoFile file;
            if (isNeedCamera) {
                file = mList.get(position - 1);
            } else {
                file = mList.get(position);
            }

            if (file.isSelected()) {
                viewHolder.mCbx.setChecked(true);
                viewHolder.mShadow.setVisibility(Component.VISIBLE);
            } else {
                viewHolder.mCbx.setChecked(false);
                viewHolder.mShadow.setVisibility(Component.INVISIBLE);
            }

            VideoPickViewHolder finalViewHolder = viewHolder;
            viewHolder.mCbx.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
                @Override
                public void onCheckedChanged(AbsButton absButton, boolean b) {
                    if (!b && isUpToMax()) {
                        ToastUtil.getInstance(mContext).showToast(ResourceTable.String_vw_up_to_max);
                        return;
                    }

                    if (!b) {
                        finalViewHolder.mShadow.setVisibility(Component.INVISIBLE);
                        finalViewHolder.mCbx.setChecked(false);
                        mCurrentNumber--;
                    } else {
                        finalViewHolder.mShadow.setVisibility(Component.VISIBLE);
                        finalViewHolder.mCbx.setChecked(true);
                        mCurrentNumber++;
                    }

                    int index = isNeedCamera ? position - 1 : position;
                    mList.get(index).setSelected(finalViewHolder.mCbx.isChecked());

                    if (mListener != null) {
                        mListener.OnSelectStateChanged(finalViewHolder.mCbx.isChecked(), mList.get(index));
                    }
                }
            });

            component.setClickedListener(component1 -> {
                Intent intent = new Intent();
                intent.setParam("videoId", file.getId());
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(mContext.getBundleName())
                        .withAbilityName(VideoAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                ((Ability) mContext).startAbility(intent);
            });

            viewHolder.mDuration.setText(Util.getDurationString(file.getDuration()));
        }

        return component;
    }

    class VideoPickViewHolder {
        private Image mIvCamera;
        private Image mIvThumbnail;
        private Component mShadow;
        private Checkbox mCbx;
        private Text mDuration;
        private DependentLayout mDurationLayout;
    }

    public boolean isUpToMax() {
        return mCurrentNumber >= mMaxNumber;
    }

    public void setCurrentNumber(int number) {
        mCurrentNumber = number;
    }
}
